 package handling.channel.handler;

 import client.MapleCharacter;
 import client.MapleClient;
 import handling.world.WorldAllianceService;
 import handling.world.WorldGuildService;
 import handling.world.guild.MapleGuild;
 import tools.MaplePacketCreator;
 import tools.data.input.LittleEndianAccessor;
 import tools.packet.GuildPacket;

 public class AllianceHandler {
   public static void HandleAlliance(LittleEndianAccessor slea, MapleClient c, boolean denied) {
     int inviteid, newGuild, gid;
/*  15 */     if (c.getPlayer().getGuildId() <= 0) {
/*  16 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/*  19 */     MapleGuild gs = WorldGuildService.getInstance().getGuild(c.getPlayer().getGuildId());
/*  20 */     if (gs == null) {
/*  21 */       c.announce(MaplePacketCreator.enableActions());
       return;
     }
/*  24 */     WorldAllianceService allianceService = WorldAllianceService.getInstance();

/*  26 */     byte op = slea.readByte();
/*  27 */     if (c.getPlayer().getGuildRank() != 1 && op != 1) {
       return;
     }
/*  30 */     if (op == 22) {
/*  31 */       denied = true;
     }
/*  33 */     int leaderid = 0;
/*  34 */     if (gs.getAllianceId() > 0) {
/*  35 */       leaderid = allianceService.getAllianceLeader(gs.getAllianceId());
     }

/*  38 */     if (op != 4 && !denied) {
/*  39 */       if (gs.getAllianceId() <= 0 || leaderid <= 0) {
         return;
       }
/*  42 */     } else if (leaderid > 0 || gs.getAllianceId() > 0) {
       return;
     }
/*  45 */     if (denied) {
/*  46 */       DenyInvite(c, gs);

       return;
     }

/*  51 */     switch (op) {
       case 1:
/*  53 */         for (byte[] pack : allianceService.getAllianceInfo(gs.getAllianceId(), false)) {
/*  54 */           if (pack != null) {
/*  55 */             c.announce(pack);
           }
         }
         return;
       case 3:
/*  60 */         newGuild = WorldGuildService.getInstance().getGuildLeader(slea.readMapleAsciiString());
/*  61 */         if (newGuild > 0 && c.getPlayer().getAllianceRank() == 1 && leaderid == c.getPlayer().getId()) {
/*  62 */           MapleCharacter chr = c.getChannelServer().getPlayerStorage().getCharacterById(newGuild);
/*  63 */           if (chr != null && chr.getGuildId() > 0 && allianceService.canInvite(gs.getAllianceId())) {
/*  64 */             chr.send(GuildPacket.sendAllianceInvite(allianceService.getAlliance(gs.getAllianceId()).getName(), c.getPlayer()));
/*  65 */             WorldGuildService.getInstance().setInvitedId(chr.getGuildId(), gs.getAllianceId());
           } else {
/*  67 */             c.getPlayer().dropMessage(1, "请确认要联盟的家族族长和您是在同一频道.");
           }
         } else {
/*  70 */           c.getPlayer().dropMessage(1, "输入的家族名字不正确，当前服务器未找到该家族的信息.");
         }
         return;
       case 4:
/*  74 */         inviteid = WorldGuildService.getInstance().getInvitedId(c.getPlayer().getGuildId());
/*  75 */         if (inviteid > 0) {
/*  76 */           if (!allianceService.addGuildToAlliance(inviteid, c.getPlayer().getGuildId())) {
/*  77 */             c.getPlayer().dropMessage(5, "An error occured when adding guild.");
           }
/*  79 */           WorldGuildService.getInstance().setInvitedId(c.getPlayer().getGuildId(), 0);
         }
         return;

       case 2:
       case 6:
/*  85 */         if (op == 6 && slea.available() >= 4L) {
/*  86 */           gid = slea.readInt();
/*  87 */           if (slea.available() >= 4L && gs.getAllianceId() != slea.readInt()) {
             return;
           }
         } else {
/*  91 */           gid = c.getPlayer().getGuildId();
         }
/*  93 */         if (c.getPlayer().getAllianceRank() <= 2 && (c.getPlayer().getAllianceRank() == 1 || c.getPlayer().getGuildId() == gid) && 
/*  94 */           !allianceService.removeGuildFromAlliance(gs.getAllianceId(), gid, (c.getPlayer().getGuildId() != gid))) {
/*  95 */           c.getPlayer().dropMessage(5, "An error occured when removing guild.");
         }
         return;

       case 7:
/* 100 */         if (c.getPlayer().getAllianceRank() == 1 && leaderid == c.getPlayer().getId() && 
/* 101 */           !allianceService.changeAllianceLeader(gs.getAllianceId(), slea.readInt())) {
/* 102 */           c.getPlayer().dropMessage(5, "An error occured when changing leader.");
         }
         return;

       case 8:
/* 107 */         if (c.getPlayer().getAllianceRank() == 1 && leaderid == c.getPlayer().getId()) {
/* 108 */           String[] ranks = new String[5];
/* 109 */           for (int i = 0; i < 5; i++) {
/* 110 */             ranks[i] = slea.readMapleAsciiString();
           }
/* 112 */           allianceService.updateAllianceRanks(gs.getAllianceId(), ranks);
         }
         return;
       case 9:
/* 116 */         if (c.getPlayer().getAllianceRank() <= 2 && 
/* 117 */           !allianceService.changeAllianceRank(gs.getAllianceId(), slea.readInt(), slea.readByte())) {
/* 118 */           c.getPlayer().dropMessage(5, "An error occured when changing rank.");
         }
         return;

       case 10:
/* 123 */         if (c.getPlayer().getAllianceRank() <= 2) {
/* 124 */           String notice = slea.readMapleAsciiString();
/* 125 */           if (notice.length() <= 100)
           {

/* 128 */             allianceService.updateAllianceNotice(gs.getAllianceId(), notice); } 
         }
         return;
     }
/* 132 */     System.out.println("Unhandled GuildAlliance op: " + op + ", \n" + slea.toString());
   }




   public static void DenyInvite(MapleClient c, MapleGuild gs) {
/* 139 */     int inviteid = WorldGuildService.getInstance().getInvitedId(c.getPlayer().getGuildId());
/* 140 */     if (inviteid > 0) {
/* 141 */       int newAlliance = WorldAllianceService.getInstance().getAllianceLeader(inviteid);
/* 142 */       if (newAlliance > 0) {
/* 143 */         MapleCharacter chr = c.getChannelServer().getPlayerStorage().getCharacterById(newAlliance);
/* 144 */         if (chr != null) {
/* 145 */           chr.dropMessage(5, "[" + gs.getName() + "] 家族拒绝了联盟的邀请.");
         }
/* 147 */         WorldGuildService.getInstance().setInvitedId(c.getPlayer().getGuildId(), 0);
       }
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\channel\handler\AllianceHandler.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */